<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pencarian_model extends CI_Model {

	function __construct()
	{
		parent::__construct();
		$this->CI = get_instance();
	}

	
	
	
	
	function get_anggaran(){
		$this->db->select("*");
		$this->db->from('ANGGARAN');
		$this->db->where("STATUS > 0 ");
		$this->db->order_by('TAHUN');
		$query = $this->db->get();
		return $query;
	}
	
	
	function get_program($id_anggaran){
		$this->db->select("*");
		$this->db->from('PROGRAM');
		$this->db->where('IDANGGARAN',$id_anggaran);
		$this->db->where("STATUS > 0 ");
		$this->db->order_by('DESKRIPSI','ASC');
		$query = $this->db->get();
		return $query;
	}
	
	
	function get_sasaran($id_program){
		$this->db->select("*");
		$this->db->from('SASARAN_PROG');
		$this->db->where('IDPROGRAM',$id_program);
		$this->db->where("STATUS > 0 ");
		$this->db->order_by('DESKRIPSI','ASC');
		$query = $this->db->get();
		return $query;
	}
	
	function get_indikator($id_sasaran){
		$this->db->select("*");
		$this->db->from('INDIKATOR_PROG');
		$this->db->where('IDSASARAN_PROG',$id_sasaran);
		$this->db->where("STATUS > 0 ");
		$this->db->order_by('DESKRIPSI','ASC');
		$query = $this->db->get();
		return $query;
	}
	
	function get_kegiatan($id_program){
		$this->db->select("*");
		$this->db->from('KEGIATAN');
		$this->db->where('IDPROGRAM',$id_program);
		$this->db->where("STATUS > 0 ");
		$this->db->order_by('DESKRIPSI','ASC');
		$query = $this->db->get();
		return $query;
	}
	
	function get_sasaran_keg($id_keg){
		$this->db->select("*");
		$this->db->from('SASARAN_KEG');
		$this->db->where('IDKEGIATAN',$id_keg);
		$this->db->where("STATUS > 0 ");
		$this->db->order_by('DESKRIPSI','ASC');
		$query = $this->db->get();
		return $query;
	}
	
	function get_indikator_keg($id_sasaran){
		$this->db->select("*");
		$this->db->from('INDIKATOR_KEG');
		$this->db->where('IDSASARAN_KEG',$id_sasaran);
		$this->db->where("STATUS > 0 ");
		$this->db->order_by('DESKRIPSI','ASC');
		$query = $this->db->get();
		return $query;
	}
	
	function get_tahun_anggaran($id){
		$this->db->select("*");
		$this->db->from('ANGGARAN');
		$this->db->where("STATUS > 0 ");
		$this->db->where('IDANGGARAN',$id);
		$this->db->order_by('TAHUN');
		$query = $this->db->get();
		return $query;
	}
	
	function get_one_kegiatan($id){
		$this->db->select("*");
		$this->db->from('KEGIATAN');
		$this->db->where('IDKEGIATAN',$id);
		$this->db->where("STATUS > 0 ");
		$query = $this->db->get();
		return $query;
	}
	
	
	function get_sasaran_indikator($id_sasaran, $id_indikator, $id_anggaran, $id_program, $operasi_t, $target){
		$this->db->select('p.DESKRIPSI as P_DESKRIPSI, p.IDPROGRAM as P_IDPROGRAM, s.IDSASARAN_PROG, s.DESKRIPSI as S_DESKRIPSI, i.*');//<<<<
		$this->db->from('SASARAN_PROG s');
		$this->db->join('INDIKATOR_PROG i','i.IDSASARAN_PROG = s.IDSASARAN_PROG', 'left');
		$this->db->join('PROGRAM p','p.IDPROGRAM = s.IDPROGRAM', 'left');//<<<<<<<<<<<<<
		$this->db->where('i.LEVEL',1);
		$this->db->where('p.IDANGGARAN',$id_anggaran);///<<<<<<<<<<<
		
		
		if($id_program==0){
			$this->db->where('s.IDPROGRAM IS NOT NULL');
		}
		if($id_program!=0){
			$this->db->where('s.IDPROGRAM',$id_program);
		}
		
				
		if($id_sasaran==0){
			$this->db->where('s.IDSASARAN_PROG IS NOT NULL');
		}
		else if($id_sasaran!=0){
			$this->db->where('s.IDSASARAN_PROG', $id_sasaran);
		}
		
		if($id_indikator==0){
			$this->db->where('i.IDINDIKATOR_PROG IS NOT NULL');
		}
		else if ($id_indikator!=0){
			$this->db->where('i.IDINDIKATOR_PROG',$id_indikator);
		}
		
		$this->db->where("s.STATUS > 0");
		$this->db->where("i.STATUS > 0");
				
		//set batasan target
		if($target!=0){
			if($operasi_t==1){
			$this->db->where('i.TARGET_DEC >=', $target);
			}
			else if($operasi_t==2){
				$this->db->where('i.TARGET_DEC', $target);
			}
			else if($operasi_t==3){
				$this->db->where('i.TARGET_DEC <=', $target);
			}
		}
		
		
		//$this->db->where('i.TARGET_DEC >', $target);//set batasan target
		$this->db->order_by('s.IDPROGRAM');
		$this->db->order_by('s.IDSASARAN_PROG');
		$this->db->order_by('i.IDINDIKATOR_PROG');
		$query = $this->db->get();
		return $query;
	}	
	
	//count program rowspan
	function get_jml_sasaran($id){
		$this->db->select('*');//<<<<
		$this->db->from('SASARAN_PROG s');
		$this->db->join('INDIKATOR_PROG i','i.IDSASARAN_PROG = s.IDSASARAN_PROG', 'left');
		$this->db->join('PROGRAM p','p.IDPROGRAM = s.IDPROGRAM', 'left');//<<<<<<<<<<<<<
		//$this->db->where('i.LEVEL',1);
		//$this->db->where('p.IDANGGARAN',$id_anggaran);///<<<<<<<<<<<
		$this->db->where('s.IDPROGRAM',$id);
		$this->db->where("p.STATUS > 0");
		$this->db->where("i.STATUS > 0");
		$this->db->where("s.STATUS > 0");
				
		$query = $this->db->get();
		return $query;
	}
	
	
	function get_jml_indikator($id_sasaran){
		$this->db->select('*');
		$this->db->from('INDIKATOR_PROG');
		$this->db->where('IDSASARAN_PROG',$id_sasaran);
		$this->db->where("STATUS > 0");
		$query = $this->db->get();
		return $query;
	}
	
	function get_real_ind_prog($id_indikator_prog, $realisasi, $operasi_r){
		$sql = "(SELECT max( UPDATED_AT ) AS max_update, BULAN FROM REALISASI_IND_PROG WHERE IDINDIKATOR_PROG = $id_indikator_prog AND STATUS>0 GROUP BY BULAN) as maxresults";
		$this->db->select('r.*');
		$this->db->from('REALISASI_IND_PROG r');
		$this->db->join($sql,'maxresults.max_update = r.UPDATED_AT');
		
		
		//set batasan realisasi
		if($realisasi!=0){			
			if($operasi_r==1){
				$this->db->where('r.PERSENTASE >=', $realisasi);
			}
			else if($operasi_r==2){
				$this->db->where('r.PERSENTASE', $realisasi);
			}
			else if($operasi_r==3){
				$this->db->where('r.PERSENTASE <=', $realisasi);
			}
		}
		//$this->db->where("r.PERSENTASE > 59");//set batasan realisasi
		$query = $this->db->get();
		return $query;
	}
	
	function get_indikator_child($id_indikator_prog){
		$this->db->select('*');
		$this->db->from('INDIKATOR_PROG');
		$this->db->where('LEVEL',2);
		$this->db->where('PARENT',$id_indikator_prog);
		$this->db->where("STATUS > 0");
		$this->db->order_by('IDINDIKATOR_PROG');
		$query = $this->db->get();
		return $query;
	}
	
	function get_programID($id_program){
		$this->db->select('a.TAHUN, a.IDANGGARAN, u.ID_UNIT_ORGANISASI, u.NAMA_UNIT, us.ID_USER, us.NAMA, us.JABATAN, p.*');
		$this->db->from('PROGRAM p');
		$this->db->join('ANGGARAN a','a.IDANGGARAN = p.IDANGGARAN');
		$this->db->join('UNIT_ORGANISASI u','u.ID_UNIT_ORGANISASI = p.ID_UNIT_ORGANISASI');
		$this->db->join('USERS us','us.ID_USER = u.DIREKTUR');
		$this->db->where('p.IDPROGRAM',$id_program);
		$query = $this->db->get();
		return $query;
	}	
	
	
	
}

?>
